package com.assem.sinoocean;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.assem.app.AppContext;
import com.assem.app.BaseActivity;
import com.assem.fragment.LightFragment;
import com.assem.fragment.RoomFragment;
import com.assem.fragment.SetupFragment;
import com.assem.utils.VerticalSlideLayout;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.myfragment)
    FrameLayout myfragment;
    @BindView(R.id.tx_setup)
    TextView txSetup;
    @BindView(R.id.tx_room)
    TextView txRoom;
    @BindView(R.id.tx_light)
    TextView txLight;
    @BindView(R.id.custom_view_pager)
    VerticalSlideLayout customViewPager;
    @BindView(R.id.tv_facility)
    TextView tvFacility;
    @BindView(R.id.tv_pattern)
    TextView tvPattern;
    private SetupFragment setupFragment = new SetupFragment();
    private RoomFragment roomFragment = new RoomFragment();
    private LightFragment lightFragment = new LightFragment();
    private Fragment[] mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        LogUtils.i(ScreenUtils.getScreenHeight());


        customViewPager.setOnPageChangeListener(new VerticalSlideLayout.OnPageChangeListener() {
            @Override
            public void onPageChange(int currentPage) {
                Toast.makeText(MainActivity.this, "第" + (currentPage + 1) + "页", Toast.LENGTH_SHORT).show();
            }
        });

        mFragments = new Fragment[3];
        //0.初始化mFragments
        mFragments[0] = setupFragment;
        mFragments[1] = roomFragment;
        mFragments[2] = lightFragment;
        //1.第一次进入页面,直接加载两个fragment
        AppContext.appSelectIndex = 1;
        FragmentUtils.add(getSupportFragmentManager(), mFragments, R.id.myfragment, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //1,每次回到承载页,都要判断当前的appSelectIndex
        if (AppContext.appSelectIndex == 1) {
            showsetup();
        } else if (AppContext.appSelectIndex == 2) {
            //显示分类
            showroom();
        } else if (AppContext.appSelectIndex == 3) {
            //显示发现
            showlight();
        }
    }

    //
    @OnClick({R.id.tx_setup, R.id.tx_room, R.id.tx_light,R.id.tv_facility, R.id.tv_pattern})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tx_setup:
                AppContext.appSelectIndex = 1;
                showsetup();
                showCurrentFragment(0);
                break;
            case R.id.tx_room://分类
                AppContext.appSelectIndex = 2;
                showroom();
                showCurrentFragment(1);
                break;
            case R.id.tx_light:
                AppContext.appSelectIndex = 3;
                showlight();
                showCurrentFragment(2);
                break;
            /**
             * 设备
             */
            case R.id.tv_facility:
                ToastUtils.showLong("点击设备,跳转设备页面");
                break;
            /**
             * 模式
             */
            case R.id.tv_pattern:
                ToastUtils.showLong("点击模式,跳转模式页面");
                break;

        }
    }


    /**
     * 控制下表设备
     */
    public void showsetup() {
        txSetup.setBackgroundResource(R.drawable.select_back);
        txRoom.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
        txLight.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
    }

    /**
     * 控制下标房间
     */
    public void showroom() {
        txRoom.setBackgroundResource(R.drawable.select_back);
        txSetup.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
        txLight.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
    }

    /**
     * 控制下表楼层
     */
    public void showlight() {
        txLight.setBackgroundResource(R.drawable.select_back);
        txRoom.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
        txSetup.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
    }

    private void showCurrentFragment(int index) {
        FragmentUtils.showHide(index, mFragments);
    }


}
